package org.ws.edu.web.services;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.ws.edu.common.model.Appointment;
import org.ws.edu.common.model.Profile;
import org.ws.edu.common.model.ProfileType;
import org.ws.edu.dao.ProfileDao;

@Controller
@RequestMapping(value = "/profile", headers = "Accept=application/json")
public class ProfileController {

    @Autowired
    private ProfileDao profileDao;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Profile getProfile(@PathVariable("id") long id) {
        return profileDao.get(id);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Profile> getProfiles() {

        Profile profile2 = new Profile();
        profile2.setName("people2");
        profile2.setPasswordHash("fsdfsdf");
        profile2.setRating(14.8);
        profile2.setType(ProfileType.EMPLOYEE);

        Appointment appointment = new Appointment();
        appointment.setName("First Appointment");
        appointment.setRating(23.0);
        appointment.setProfile(profile2);

        Set<Appointment> appointments = new HashSet<Appointment>();
        appointments.add(appointment);

        profile2.setAppointments(appointments);

        profileDao.create(profile2);

        return profileDao.getList();

    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @Transactional(rollbackFor = Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public void deleteProfile(@RequestBody Profile profile) {
        profileDao.delete(profile);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @Transactional(rollbackFor = Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public void deleteProfile(@PathVariable("id") long id) {
        profileDao.delete(id);
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @Transactional(rollbackFor = Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public void updateProfile(@RequestBody Profile profile) {
        profileDao.update(profile);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    @ResponseStatus(HttpStatus.CREATED)
    public void saveProfile(@RequestBody Profile profile) {
        profileDao.create(profile);
    }
}
